package de.lianowar.whereismymoney.widgets;


import de.lianowar.whereismymoney.R;
import de.lianowar.whereismymoney.sql.DatabaseHandler;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RemoteViews;

public class WhereIsMyMoneyWidget extends AppWidgetProvider {

	public static String YOUR_AWESOME_ACTION = "YourAwesomeAction";
	public static String YOUR_AWESOME_ACTION2 = "YourAwesomeAction2";
    private AlarmManager mAlarmManager;
    private Intent mIntent;
    private PendingIntent mPendingIntent;
    private SharedPreferences prefs;
    
	public WhereIsMyMoneyWidget() {}
    private  DatabaseHandler dbHandler;
	  public void onUpdate(Context context, AppWidgetManager appWidgetManager,  int[] appWidgetIds)
	  {
		     Log.e("WIDGET", "onUpdate()");
		     prefs = context.getSharedPreferences("de.lianowar.whereismymoney",0);
		     
		     int test = prefs.getInt("widget1", 0);
		     int test1 = prefs.getInt("widget2", 0);
		     
		     Log.e("Setting1", ""+test);
		     Log.e("Setting2", ""+test1);
		  	 RemoteViews remoteViews;
	         ComponentName watchWidget;
	         dbHandler = new DatabaseHandler(context);

	         remoteViews = new RemoteViews( context.getPackageName(), R.layout.whereismymoneywidget );
	         watchWidget = new ComponentName( context, WhereIsMyMoneyWidget.class );
	         remoteViews.setTextViewText( R.id.tvAccountNameWid,""+ dbHandler.getAccountById(test) );
	         remoteViews.setTextViewText( R.id.tvAccountName1Wid,""+ dbHandler.getAccountById(test1)+ "€");
	         remoteViews.setTextViewText( R.id.tvMoneyAmount,""+ dbHandler.sumMoneyAmount(test) + "€");
	         remoteViews.setTextViewText( R.id.tvMoneyAmount1,""+ dbHandler.sumMoneyAmount(test1)+ "€");
	         appWidgetManager.updateAppWidget( watchWidget, remoteViews );
	         //toTest
	         
	         if(mAlarmManager==null) {
	             //Log.v(this.getClass().getName(), "Launching Clock Widget. Initiating...");
	             mAlarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
	             mIntent = new Intent();
	             mIntent.setAction(YOUR_AWESOME_ACTION);
	             mPendingIntent = PendingIntent.getBroadcast(context, 0, mIntent, 0);
	             mAlarmManager.setRepeating(AlarmManager.RTC, (long)0, (long)600000, mPendingIntent);
	         }
	       
	  }
	  @Override
	  public void onReceive(Context context, Intent intent) {
	   super.onReceive(context, intent);
	   Log.e("WIDGET", "onReceive()");
	   dbHandler = new DatabaseHandler(context);
	   	 prefs = context.getSharedPreferences("de.lianowar.whereismymoney",0);
	     int test = prefs.getInt("widget1", 0);
	     int test1 = prefs.getInt("widget2", 0);
	     Log.e("Setting1", ""+test);
	     Log.e("Setting2", ""+test1);
	   if(intent.getAction().equals(YOUR_AWESOME_ACTION))
	   {
		   
		   if(dbHandler != null)
		   {
			   Log.e("WIDGET", "dbHandler != null");
			   RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.whereismymoneywidget);
			   rv.setTextViewText( R.id.tvAccountNameWid,""+ dbHandler.getAccountById(test) );
			   rv.setTextViewText( R.id.tvAccountName1Wid,""+ dbHandler.getAccountById(test1));
			   rv.setTextViewText( R.id.tvMoneyAmount,""+ dbHandler.sumMoneyAmount(test) +" €");
			   rv.setTextViewText( R.id.tvMoneyAmount1,""+ dbHandler.sumMoneyAmount(test1)+" €");
		       AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		       ComponentName thisWidget = new ComponentName(context, WhereIsMyMoneyWidget.class);
		       appWidgetManager.updateAppWidget(thisWidget, rv);
		   }
		   else
		   {
			   Log.e("WIDGET", "dbHandler == null");
		   
		   }
		
	   }
	   else if(intent.getAction().equals(YOUR_AWESOME_ACTION2))
	   {	   
		   Log.e("WIDGET", "dbHandler != null");
		   RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.whereismymoneywidget);
		   rv.setTextViewText( R.id.tvAccountNameWid,""+ dbHandler.getAccountById(test) );
		   rv.setTextViewText( R.id.tvAccountName1Wid,""+ dbHandler.getAccountById(test1));
		   rv.setTextViewText( R.id.tvMoneyAmount,""+ dbHandler.sumMoneyAmount(test) +" €");
		   rv.setTextViewText( R.id.tvMoneyAmount1,""+ dbHandler.sumMoneyAmount(test1)+" €");
	       AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
	       ComponentName thisWidget = new ComponentName(context, WhereIsMyMoneyWidget.class);
	       appWidgetManager.updateAppWidget(thisWidget, rv);
	   }
	   dbHandler.close();
	  }	  
	   
}
